package com.yueya.basic.news.content.action;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;
import com.yueya.basic.dict.model.Dict;
import com.yueya.basic.news.content.model.Content;
import com.yueya.basic.news.subject.model.Subject;
import com.yueya.common.action.BaseController;
import com.yueya.common.model.Json;
import com.yueya.common.util.UploadUtil;
import com.yueya.config.GlobalParam;

/**
 * 新闻内容
 * 
 * @author Administrator
 * 
 */
public class ContentController extends BaseController {

	Map<String, Object> map = new HashMap<String, Object>();

	public void index() {
		this.setAttr("flag", "0");
		this.setAttr("subject", Subject.service.getList());
		render("/WEB-INF/views/basic/news/content/list.jsp");
	}

	public void getListPager() {
		Record user = getUserWithCookie();
		String userid = user.getStr("id");

		map.clear();
		map.put("name", this.getPara("name"));
		map.put("flag", this.getPara("flag"));
		map.put("userid", userid);
		map.put("subjectid", this.getPara("subjectid"));
		Page<Record> page = Content.service.pagination(getPage(), getRows(), map);
		jsonMap.put("total", page.getTotalRow());
		jsonMap.put("rows", page.getList());
		renderJson(JsonKit.toJson(jsonMap));
	}

	public void doAdd() {
		Record user = getUserWithCookie();
		String userid = user.getStr("user_id");
		String usernam = user.getStr("user_name");

		this.setAttr("userid", userid);
		this.setAttr("usernam", usernam);

		this.render("/WEB-INF/views/basic/news/content/edit.jsp");
	}

	public void doEdit() {
		String id = this.getPara("id");

		Record content = Content.service.getContentById(id);

		setAttr("content", content);
		setAttr("visit_path", GlobalParam.getAttUrl());
		this.render("/WEB-INF/views/basic/news/content/edit.jsp");
	}

	public void save() throws Exception {
		Content c = getModel(Content.class);
		String content = this.getPara("contenthtml");
		String contentText = this.getPara("contenttext");
//		File upFile = new File(PropKit.get("upload.path"));
//
//		if (!upFile.getParentFile().exists()) {
//			upFile.getParentFile().mkdirs();
//		}
		boolean bl = Content.service.save(c, content, contentText);

		Json j = new Json();
		if (bl) {
			j.setSuccess(true);
			j.setMsg("提交成功");
		} else {
			j.setSuccess(false);
			j.setMsg("操作失败");
		}

		this.renderJson(j);
	}

	public void remove() {
		int num = Db.update("update t_content set del_flag='1' where content_id =?", this.getPara("id"));
		Json j = new Json();
		if (num > 0) {
			j.setSuccess(true);
			j.setMsg("删除成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("删除失败!");
		}
		this.renderJson(j);
	}

	public void bathremove() {
		Boolean bl = false;

		String idsStr = this.getPara("ids");

		if (idsStr != null) {
			String[] ids = idsStr.split(";");
			Content tzgg = null;
			for (int i = 0; i < ids.length; i++) {
				if (!"".equals(ids[i]) && ids[i] != null) {
					tzgg = Content.dao.findById(ids[i]);
					tzgg.set("del_flag", 1);
					if (tzgg.update()) {
						bl = true;
					} else {
						bl = false;
						break;
					}
				}

			}
		}

		Json j = new Json();
		if (bl == true) {
			j.setSuccess(true);
			j.setMsg("删除成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("删除失败!");
		}
		this.renderJson(j);
	}
	public void uploadImg() {
		Content c = Content.dao.findById(getPara("contentId"));
		this.setAttr("content", c);
		setAttr("visit_path", GlobalParam.getAttUrl());
		this.render("/WEB-INF/views/basic/news/content/uploadimg.jsp");
	}
	public void saveUploadImg() {
		Content c = getModel(Content.class, "content");
		boolean bl = c.update();
		Json j = new Json();
		if (bl) {
			j.setSuccess(true);
			j.setMsg("提交成功");
		} else {
			j.setSuccess(false);
			j.setMsg("操作失败");
		}

		this.renderJson(j);
	}
	
	// 图片上传
	public void uploadInfo() {
		Map<String, Object> uploadMap = null;
		UploadFile upfile = getFile();
		System.out.println("typestr:" + this.getPara("typestr"));
		if (StrKit.notNull(upfile)) {
			File file = upfile.getFile();
			String typeStr = this.getPara("typestr");
			String filedir = "/other"; // 存放目录
			if ("img_picker".equals(typeStr)) {
				filedir = "/image";
			}

			uploadMap = UploadUtil.uploadFile(Dict.dao.getValue("attachment_upload_save_path"), upfile.getFileName(), filedir, file);
			if ("1".equals(uploadMap.get("flag").toString())) {
				String savePath = (String) uploadMap.get("savepath");
				uploadMap.put("savepath", GlobalParam.getAttUrl() + savePath);
				uploadMap.put("relativepath", savePath);
				uploadMap.put("filename", upfile.getFileName());

				file.delete();
			}

		}
		renderJson(uploadMap);

	}

}
